
import { transformScaleConst } from "@/visual/commons/consts/dashboard-const";

/**
 * 仪表板工具类
 */
export default class DashboardUtil {

    /**
     * 删除树的子节点
     * @param list 
     * @param id 
     * @param succsss 
     */
    static deleteTreeChildrenNodeById(list: any[], id: string, succsss: Function) {
        for (let i = 0; i < list.length; i++) {
            if (list[i].id === id) {
                const delNode = list.splice(i, 1);
                succsss(delNode[0]);
                break;
            }

            if (list[i].children) {
                DashboardUtil.deleteTreeChildrenNodeById(list[i].children, id, succsss);
            }
        }
    }


    /**
     * 获取节点
     * @param list 
     * @param comp 
     * @returns 
     */
    static getTreeNodeById(list: any[], id: string, resolve: any) {
        for (let i = 0; i < list.length; i++) {
            if (list[i].id === id) {
                resolve(list[i]);
                break;
            }

            if (list[i].children) {
                DashboardUtil.getTreeNodeById(list[i].children, id, resolve);
            }
        }

        return null;
    }

    /**
     * 获取节点索引与当前列表
     * @param list 
     * @param comp 
     * @returns 
     */
    static getTreeNodeIndexAndListById(list: any[], id: string, resolve: any) {
        for (let i = 0; i < list.length; i++) {
            if (list[i].id === id) {
                resolve(i, list);
                break;
            }

            if (list[i].children) {
                DashboardUtil.getTreeNodeIndexAndListById(list[i].children, id, resolve);
            }
        }

        return null;
    }

    /**
     * 树节点是否选中
     * @param list 
     * @param resolve 
     * @returns 
     */
    static treeNodeIsChecked(list: any[], resolve: any) {
        for (let i = 0; i < list.length; i++) {
            if (list[i].checked) {
                resolve(true);
                break;
            }

            if (list[i].children) {
                DashboardUtil.treeNodeIsChecked(list[i].children, resolve);
            }
        }

        return null;
    }

    /**
     * 删除选中的节点
     */
    static deleteCheckedTreeNodes(list: any[], succsss: Function) {
        for (let i = list.length - 1; i >= 0; i--) {
            if (list[i].checked) {
                const delNode = list.splice(i, 1);
                succsss(delNode[0]);
            }

            if (list[i] && list[i].children) {
                DashboardUtil.deleteCheckedTreeNodes(list[i].children, succsss);
            }
        }
    }

    /**
     * 交换数组元素
     */
    static swapArrayItem(list: any[], index1: number, index2: number) {
        list[index1] = list.splice(index2, 1, list[index1])[0]
        return list
    }

    /**
     * 根据id上移节点
     * @param list 
     * @param comp 
     * @returns 
     */
    static treeNodeUpById(list: any[], id: string, resolve: any) {
        if (list.length <= 1) {
            return;
        }

        for (let i = list.length - 1; i >= 0; i--) {
            if (list[i] && list[i].id === id && i > 0) {
                const source = {
                    ...list[i]
                };
                list[i] = {
                    ...list[i - 1]
                };
                list[i - 1] = source;
                resolve(true);
                break;
            }

            if (list[i] && list[i].children) {
                DashboardUtil.getTreeNodeById(list[i].children, id, resolve);
            }
        }

    }



    /**
     * 根据id下移节点
     * @param list 
     * @param comp 
     * @returns 
     */
    static treeNodeDownById(list: any[], id: string, resolve: any) {
        if (list.length <= 1) {
            return;
        }

        for (let i = 0; i < list.length; i++) {
            if (list[i] && list[i].id === id && i < list.length - 1) {
                const source = {
                    ...list[i]
                };
                list[i] = {
                    ...list[i + 1]
                };
                list[i + 1] = source;
                resolve(true);
                break;
            }

            if (list[i] && list[i].children) {
                DashboardUtil.getTreeNodeById(list[i].children, id, resolve);
            }
        }

    }

    /**
     * 根据id置顶节点
     * @param list 
     * @param comp 
     * @returns 
     */
    static treeNodeToTopById(list: any[], id: string, resolve: any) {
        if (list.length <= 1) {
            return;
        }
        DashboardUtil.getTreeNodeIndexAndListById(list, id, (index: number, arr: any[]) => {
            const nodes = arr.splice(index, 1);
            arr.push(nodes[0]);
            resolve(true);
        })
    }

    /**
     * 根据id置底节点
     * @param list 
     * @param comp 
     * @returns 
     */
    static treeNodeToBottomById(list: any[], id: string, resolve: any) {
        if (list.length <= 1) {
            return;
        }
        DashboardUtil.getTreeNodeIndexAndListById(list, id, (index: number, arr: any[]) => {
            const nodes = arr.splice(index, 1);
            arr.unshift(nodes[0]);
            resolve(true);
        })
    }

    /**
     * 移动到指定位置
     * @param list 
     * @param comp 
     * @returns 
     */
    static treeNodeMoveToIndex(list: any[], id: string, targetIndex: number, resolve: any) {
        if (list.length <= 1) {
            return;
        }
        DashboardUtil.getTreeNodeIndexAndListById(list, id, (index: number, arr: any[]) => {
            const source = {
                ...arr[index]
            };
            arr[index] = {
                ...arr[targetIndex]
            };

            arr[targetIndex] = source;
            resolve(true);
        })
    }

    /**
     * 指定位置添加节点 
     * @param list 
     * @param index 
     * @param value 
     */
    static treeInsertNode(list: any[], index: number, value: any) {
        list.splice(index, 0, value);
    }

    /**
     * 翻转值获取
     */
    static geetTransformScaleCssVal(val: number):string {
        const list = Object.keys(transformScaleConst);
        for (let i = 0; i < list.length; i++) {
            const item = transformScaleConst[list[i]];
            if (item && item.val === val) {
                return item.cssVal;
            }
        }

        return '';
    }
}